#pragma once

#include <mysql/mysql.h>

class Iterator
{
public:
    Iterator(MYSQL_RES *res) : res_(res), row_(mysql_fetch_row(res_)), num_fields_(mysql_num_fields(res_)){};
    Iterator() : res_(nullptr), row_(nullptr), num_fields_(0){};

public:
    const char *operator[](size_t index) const
    {
        return (index < num_fields_) ? row_[index] : nullptr;
    }

    Iterator &operator++()
    {
        row_ = mysql_fetch_row(res_);
        return *this;
    }

    bool operator!=(const Iterator &other) const
    {
        return row_ != other.row_;
    }

    const Iterator &operator*() const
    {
        return *this;
    }

private:
    MYSQL_RES *res_;
    MYSQL_ROW  row_;
    uint32_t   num_fields_;
};

class MySQLResult
{
public:
    MySQLResult(MYSQL_RES *res);
    ~MySQLResult();

public:
    Iterator begin();
    Iterator end();
    uint64_t rowCount() const;
    uint32_t fieldCount() const;

private:
    MYSQL_RES *res_;
};